package com.example.demoes.web;

import com.example.demoes.entity.GoodsInfo;
import com.example.demoes.esrepo.GoodsInfoRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@Slf4j
public class HomeController {

    @Autowired
    private GoodsInfoRepository goodsInfoRepository;

    @GetMapping({"", "index"})
    public String index() {
        return "index";
    }


    @PostMapping("save")
    public String save(GoodsInfo goodsInfo) {

        try {
            goodsInfo.setId(System.currentTimeMillis());
            goodsInfoRepository.save(goodsInfo);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }

        return "index";
    }


    @GetMapping("list")
    @ResponseBody
    public Object list(){
        return goodsInfoRepository.findAll();
    }
}
